from django.http import *
from models_goods.oo import *
from django.db.models import F

# Create your views here.


def asset_turn(request):

    if request.method == 'GET':
        stockcontrol_id = request.GET.get('stockcontrol_id')
        try:
            sc_obj = StockControl.objects.get(pk=stockcontrol_id)
        except StockControl.DoesNotExist:
            return HttpResponse('没有这个明细')
        else:
            all_room = AllRoomsName().get_info_detail()

            all_user = AllUserName().get_info()

            context = {'sc_obj': sc_obj,
                       'all_room': all_room,
                       'all_user': all_user}

            return render(request, 'asset_turn/asset_turn.html', context)

    elif request.method == 'POST':

        info = request.POST

        room = info.get('room')

        user = info.get('user')

        number = int(info.get('number'))

        unit = info.get('unit')

        sc_obj_id = info.get('sc_obj_id')

        sc_obj = StockControl.objects.get(pk=sc_obj_id)
        if sc_obj.number < number:
            return HttpResponse('数量不足')
        elif number < 0:
            return HttpResponse('数量不可为负')
        sc_obj.number = F('number') - number
        sc_obj.save()

        try:
            add_obj = StockControl.objects.get(room_id=room,
                                               user_id=user,
                                               unit=unit,
                                               goods_id=sc_obj.goods_id,)
            # 保存流水到数据库中
            SaveRunningWater(user_id=user, room=sc_obj.room_id, room_to=room) \
                .save_obj(sc_obj.goods, number, unit, User.objects.get(name=request.session['name']).id)
            # save_obj是oo.py内的函数

            # 流水详情，从流水表中获取数据
            # 资产名称，转移详情，保管人，录入人员，录入日期
            all_run_water = RunningWater.objects.all().values_list(
                'goods__name', 'detail', 'user__name', 'ctrl_user__name', 'pub_date')

        except StockControl.DoesNotExist:
            StockControl.objects.create(room_id=room,
                                        user_id=user,
                                        unit=unit,
                                        number=number,
                                        goods_id=sc_obj.goods_id,)
            
            # 保存流水到数据库中
            SaveRunningWater(user_id=user, room=sc_obj.room_id, room_to=room)\
                .save_obj(sc_obj.goods, number, unit, User.objects.get(name=request.session['name']).id)
            # save_obj是oo.py内的函数

            # 流水详情，从流水表中获取数据
            # 资产名称，转移详情，保管人，录入人员，录入日期
            all_run_water = RunningWater.objects.all().values_list(
                'goods__name', 'detail', 'user__name', 'ctrl_user__name', 'pub_date')

            context = {
                "all_run_water": all_run_water,
            }

            return render(request, 'asset_turn/流水展示.html', context)

        else:
            context = {"all_run_water": all_run_water, }
            add_obj.number = F('number')+number
            add_obj.save()
            return render(request, 'asset_turn/流水展示.html', context)


# 资产转移流水展示
def run_water(request):
    # 流水详情，从流水表中获取数据
    # 资产名称，转移详情，保管人，录入人员，录入日期
    all_run_water = RunningWater.objects.all().values_list(
        'goods__name', 'detail', 'user__name', 'ctrl_user__name', 'pub_date')

    context = {"all_run_water": all_run_water, }
    return render(request, 'asset_turn/流水展示.html', context)            

            

            


        

        




























        

    
